package cn.com.cifi.mars.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.cifi.mars.constant.Constants;
import cn.com.cifi.mars.entity.CooperaTionCurVersion;
import cn.com.cifi.mars.entity.StagePartNers;
import cn.com.cifi.mars.mapper.mars.StagePartNersMapper;
import cn.com.cifi.mars.service.MdmService;
import cn.com.cifi.mars.service.StagePartNersService;
import cn.com.cifi.mars.util.LocalDateTimeUtils;
import cn.com.cifi.mars.util.UUIDUtils;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 分期合作方同步表 服务实现类
 * </p>
 *
 * @author plx
 * @since 2020-01-10
 */
@Slf4j
@Service
public class StagePartNersServiceImpl extends ServiceImpl<StagePartNersMapper, StagePartNers> implements StagePartNersService {

	@Autowired
	private StagePartNersMapper stagePartNersMapper;
	@Autowired
	private MdmService mdmService;
	
	/**
	 * 分期合作方同步Rest接口
	 */
	@Override
	public String syncStagePartners(JSONObject requestInfo) {
		List<StagePartNers>list=new ArrayList<>();
		JSONObject projectStages = requestInfo.getJSONObject("projectStages");
		JSONArray projectStagesInfoLine = projectStages.getJSONArray("projectStagesInfoLine");
		for (Object projectStagesInfoLineObject : projectStagesInfoLine) {
			JSONObject projectStagesInfoLineObj = (JSONObject) projectStagesInfoLineObject;
			String taxType = projectStagesInfoLineObj.getString("taxType");
			String isValid = projectStagesInfoLineObj.getString("isValid");
			String remark = projectStagesInfoLineObj.getString("remark");
			String stageOldName = projectStagesInfoLineObj.getString("stageOldName");
			String setupTime = projectStagesInfoLineObj.getString("setupTime");
			String status = projectStagesInfoLineObj.getString("status");
			String stagesFlag = projectStagesInfoLineObj.getString("stagesFlag");
			String projectId=projectStagesInfoLineObj.getString("projectID");
			String kingdeeProjectId=projectStagesInfoLineObj.getString("kingdeeProjectID");
			String projectCode = projectStagesInfoLineObj.getString("projectCode");
			String createdTime =  projectStagesInfoLineObj.getString("createdTime");
			String updatedTime =  projectStagesInfoLineObj.getString("updatedTime");
			String createdBy =  projectStagesInfoLineObj.getString("createdBy");
			String updatedBy =  projectStagesInfoLineObj.getString("updatedBy");

			JSONArray stagePartnerInfoLine=projectStagesInfoLineObj.getJSONArray("stagePartnerInfoLine");
			for (Object object : stagePartnerInfoLine) {
				StagePartNers sp=new StagePartNers();
				JSONObject obj = (JSONObject) object;
				//String projectID = obj.getString("projectID");
				//String kingdeeProjectID = obj.getString("kingdeeProjectID");
				//String projectCode = obj.getString("projectCode");
				String tradeMode = obj.getString("tradeMode");
				String partnerNo =  obj.getString("partnerNo");
				String partner =  obj.getString("partner");
				String isProjectManager =  obj.getString("isProjectManager");
				String partnerIsAndTable =  obj.getString("partnerIsAndTable");
				String equityRatio =  obj.getString("equityRatio");
				String isOPProcurement =  obj.getString("isOPProcurement");
				String isOPFinance =  obj.getString("isOPFinance");
				String isOPCost =  obj.getString("isOPCost");
				String isOPLegal =  obj.getString("isOPLegal");
				String isOPProject =  obj.getString("isOPProject");
				String isOPOffice =  obj.getString("isOPOffice");
				String isOPService =  obj.getString("isOPService");
				String isOPMating =  obj.getString("isOPMating");
				String isOPDesign =  obj.getString("isOPDesign");
				String isHoldManagement =  obj.getString("isHoldManagement");
				String isOPMarketing =  obj.getString("isOPMarketing");
				String isOPOperation =  obj.getString("isOPOperation");
				String tradeObjName =  obj.getString("tradeObjName");
//				String createdTime =  obj.getString("createdTime");
//				String updatedTime =  obj.getString("updatedTime");
//				String createdBy =  obj.getString("createdBy");
//				String updatedBy =  obj.getString("updatedBy");
				String versionId =  obj.getString("versionId");
				String projectName =  obj.getString("projectName");
				String stageCode =  obj.getString("stageCode");
				String projectFID =  obj.getString("projectFID");
				String stageName =  obj.getString("stageName");
				String kingdeeProjectFID =  obj.getString("kingdeeProjectFID");
				String isDelete =  obj.getString("isDelete");
				
				sp.setProjectId(projectId);
				sp.setKingdeeProjectId(kingdeeProjectId);
				sp.setProjectCode(projectCode);
				sp.setTradeMode(tradeMode);
				sp.setPartNerNo(partnerNo);
				sp.setPartNer(partner);
				sp.setIsProjectManager(isProjectManager);
				sp.setPartNerIsAndTable(partnerIsAndTable);
				sp.setEquityRatio(equityRatio);
				sp.setIsOppRocurement(isOPProcurement);
				sp.setIsOpFinance(isOPFinance);
				sp.setIsOpCost(isOPCost);
				sp.setIsOpLegal(isOPLegal);
				sp.setIsOpProject(isOPProject);
				sp.setIsOpOffice(isOPOffice);
				sp.setIsOpService(isOPService);
				sp.setIsOpMating(isOPMating);
				sp.setIsOpDesign(isOPDesign);
				sp.setIsHoldManagement(isHoldManagement);
				sp.setIsOpMarketing(isOPMarketing);
				sp.setIsOpOperation(isOPOperation);
				sp.setTradeObjName(tradeObjName);
				if(StringUtils.isNotBlank(createdTime)) {
					LocalDateTime ldt=LocalDateTimeUtils.analysisDate(createdTime);
					sp.setCreatedTime(ldt);
				}
				
				if(StringUtils.isNotBlank(updatedTime)) {
					LocalDateTime ldt=LocalDateTimeUtils.analysisDate(updatedTime);
					sp.setUpdatedTime(ldt);
				}
				if(StringUtils.isNotBlank(setupTime)) {
					LocalDateTime ldt=LocalDateTimeUtils.analysisDate(setupTime);
					sp.setSetupTime(ldt);
				}
				
				sp.setCreatedBy(createdBy);
				sp.setUpdatedBy(updatedBy);
				sp.setVersionId(versionId);
				sp.setProjectName(projectName);
				sp.setStageCode(stageCode);
				sp.setProjectfId(projectFID);
				sp.setStageName(stageName);
				sp.setKingdeeProjectfId(kingdeeProjectFID);
				sp.setIsDelete(isDelete);
				sp.setTaxType(taxType);
				sp.setIsValid(isValid);
				sp.setRemark(remark);
				sp.setStageOldName(stageOldName);
				sp.setStatus(status);
				sp.setStagesFlag(stagesFlag);
				sp.setId(UUIDUtils.create());
				list.add(sp);
			}
		}
		//批量插入
		boolean flag=this.saveBatch(list);
		if(flag) {
			return "插入成功";
		}else {
			return "插入失败";
		}
	}
	
	/**
	 * 分期合作方同步视图
	 * @return
	 */
	@Override
	public String syncViewStagePartners() {
		List<CooperaTionCurVersion>list=this.getCooperaTionCurVersionList();
		List<StagePartNers>spList=new ArrayList<>();
		if(CollectionUtils.isNotEmpty(list)) {
			for (CooperaTionCurVersion cooperaTionCurVersion : list) {
				StagePartNers sp=new StagePartNers();
				try {
					//复制视图类 到分期合作方类
					BeanUtils.copyProperties(sp,cooperaTionCurVersion);
					spList.add(sp);
				} catch (IllegalAccessException e) {
					log.error("###### errorMsg: ", e);
				} catch (InvocationTargetException e) {
					log.error("###### errorMsg: ", e);
				}
			}
			boolean flag=this.saveOrUpdateBatch(spList);
			return flag+"";
		}
		return false+"";
	}
	
	
	/**
	 *  查询分期合作方视图
	 * @return
	 */
	public List<CooperaTionCurVersion> getCooperaTionCurVersionList() {
		List<CooperaTionCurVersion>list=mdmService.queryCooperaTionCurVersionList();
		return list;
	}

	/**
	 * 根据分期id获取主数据分期合作方同步表
	 */
	@Override
	public StagePartNers getStagePartnersByProjectFid(String projectfId) {
		return stagePartNersMapper.selectStagePartnersByProjectFid(projectfId, Constants.PARTNER);
	}

}
